package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.PinYinUtil;
import cn.stylefeng.guns.sys.modular.system.entity.CwKjkm;
import cn.stylefeng.guns.sys.modular.system.mapper.CwKjkmMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwKjkmParam;
import cn.stylefeng.guns.sys.modular.system.model.result.CwKjkmResult;
import cn.stylefeng.guns.sys.modular.system.service.CwKjkmService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 会计科目表 服务实现类
 * </p>
 *
 * @author gong
 * @since 2021-02-01
 */
@Service
public class CwKjkmServiceImpl extends ServiceImpl<CwKjkmMapper, CwKjkm> implements CwKjkmService {

    @Override
    public void add(CwKjkmParam cwKjkmParam) {
        //完善pids,根据pid拿到pid的pids
        this.treeSetPids(cwKjkmParam);

        //补充创建人信息
        cwKjkmParam.setCreateUser(LoginContextHolder.getContext().getUserId());
        cwKjkmParam.setCreateUserView(LoginContextHolder.getContext().getUser().getName());
        //科目级别
        String pids = cwKjkmParam.getPids();
        int leveal = pids.split(",").length;
        cwKjkmParam.setLevel(leveal+"");
        CwKjkm entity = getEntity(cwKjkmParam);
        String pinInCode = PinYinUtil.getPinYinCode(entity.getName());
        entity.setZjm(pinInCode);
        this.save(entity);
    }

    /**
     * 设置父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void treeSetPids(CwKjkmParam cwKjkmParam) {
        if (ToolUtil.isEmpty(cwKjkmParam.getPid()) || cwKjkmParam.getPid().equals(0L)) {
            cwKjkmParam.setPid(0L);
            cwKjkmParam.setPids("[0],");
        } else {
            Long pid = cwKjkmParam.getPid();
            CwKjkm temp = this.getById(pid);
            String pids = temp.getPids();
            cwKjkmParam.setPid(pid);
            cwKjkmParam.setPids(pids + "[" + pid + "],");
        }
    }

    @Override
    public void delete(CwKjkmParam param) {
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CwKjkmParam param) {
        CwKjkm oldEntity = getOldEntity(param);
        //修改前级别
        String oldLevel = oldEntity.getLevel();
        //修改前父级
        Long oldPid = oldEntity.getPid();

        //完善pids,根据pid拿到pid的pids
        this.treeSetPids(param);
        int leveal = param.getPids().split(",").length;
        param.setLevel(leveal+"");
        CwKjkm newEntity = getEntity(param);
        //修改后级别
        String newLevel = newEntity.getLevel();
        //修改后父级
        Long newPid = newEntity.getPid();

        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setUpdateUserView(LoginContextHolder.getContext().getUser().getName());
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        //二级科目父级变化之后，重新计算所有门店的投资情况的金额
        //TODO-1 修改前后都是二级，父级不同
//        if(oldLevel.equals(newLevel)&&oldLevel.equals("2")&&!oldPid.equals(newPid)){
//            //重新计算所有门店的投资情况金额
//            tzqkService.updateAllMdJineHeJi();
//        }


    }

    @Override
    public CwKjkmResult findBySpec(CwKjkmParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<CwKjkmResult> findListBySpec(CwKjkmParam param) {
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CwKjkmParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CwKjkmParam param) {
        return param.getBtyCwKjkmId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CwKjkm getOldEntity(CwKjkmParam param) {
        return this.getById(getKey(param));
    }

    private CwKjkm getEntity(CwKjkmParam param) {
        CwKjkm entity = new CwKjkm();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public CwKjkm getDetailById(Long btyCwKjkmId) {
        CwKjkm detail = this.getById(btyCwKjkmId);
        if (detail.getPid() != 0L && detail != null) {
            CwKjkm parentDetail = this.getById(detail.getPid());
            if (parentDetail != null) {
                detail.setPName(parentDetail.getName());
            }
        }

        return detail;
    }

}
